<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* @framework : Codeigniter Framework
*
* @author: fatangel26
*
*/
	class Slide extends Admin_Controller
	{
		public function __construct()
		{
			parent::__construct();

            if(!$this->my_session->is_Admin())
            {
                redirect(base_url()."backend/auth/login");

                exit();
            }

            $this->load->model("backend/slide_model", "MSlide", TRUE);
		}

        public function index()
        {
            $this->_data['title'] = 'Jewel | Slideshow';

            $data['slides'] = $this->MSlide->select();

            if(!$data['slides'])
            {
                $data['msg'] = 'Không có dữ liệu để hiển thị.';
            }

            $this->_data['content'] = $this->load->view("backend/slide/listslide", $data, TRUE);

            $this->load->view("backend/layout", $this->_data);
        }


        public function createslide()
        {
            $this->_data['title'] = 'Jewel | Thêm mới slide';

            /*Validate data*/
            $this->form_validation->set_rules('slide_link', 'Liên kết', 'trim|required|xss_clean');
            $this->form_validation->set_rules('slide_image', 'Đường dẫn ảnh', 'trim|required|xss_clean');
            $this->form_validation->set_rules('slide_image_thumb', 'Đường dẫn thumbnail', 'trim|required|xss_clean');
            $this->form_validation->set_rules('slide_des', 'Mô tả slide', 'trim|required|xss_clean');
            $this->form_validation->set_rules('slide_order', 'Thứ tự slide', 'trim|required|numeric|greater_than[-1]|xss_clean');
            $this->form_validation->set_message('required','<span style="font-weight: bold">%s</span> không được để trống');
            $this->form_validation->set_message('min_length','<span style="font-weight: bold">%s</span> không được ít hơn 5 ký tự');
            $this->form_validation->set_message('greater_than','<span style="font-weight: bold">%s</span> phải lớn hơn 0');

            if($this->form_validation->run() == FALSE)
            {
                $data['msg'] = '';
            }
            else
            {
                $strInsert = array(
                    'slide_link'        => $this->input->post('slide_link', TRUE),
                    'slide_image'       => $this->input->post('slide_image', TRUE),
                    'slide_image_thumb' => $this->input->post('slide_image_thumb', TRUE),
                    'slide_order'       => $this->input->post('slide_order', TRUE),
                    'slide_status'      => $this->input->post('slide_status', TRUE),
                    'slide_des'         => $this->input->post('slide_des', TRUE)
                );

                $check = $this->MSlide->create($strInsert);

                if(!$check)
                {
                    $data['msg'] = 'Lỗi khởi tạo.';
                }
                else
                {
                    $this->native_session->set_flashdata('msg', 'Thêm slide thành công.');

                    redirect(base_url()."backend/slide");

                    exit();
                }
            }

            $this->_data['content'] = $this->load->view("backend/slide/createslide", $data='', TRUE);

            $this->load->view("backend/layout", $this->_data);
        }//End function createslide

        public function editslide()
        {
            $this->_data['title'] = 'Jewel | Cập nhật slide';

            if(!$this->uri->segment(4) || $this->uri->segment(4) == '')
            {
                show_404();

                exit();
            }

            $slide_id = $this->uri->segment(4);

            $data['slide'] = $this->MSlide->select(array('slide_id' => $slide_id));

            if(!$data['slide'])
            {
                $data['msg'] = 'Không tồn tại slide này.';
            }
            else
            {

                /*Validate data*/
                $this->form_validation->set_rules('slide_link', 'Liên kết', 'trim|required|xss_clean');
                $this->form_validation->set_rules('slide_image_edit', 'Đường dẫn ảnh', 'trim|required|xss_clean');
                $this->form_validation->set_rules('slide_image_thumb_edit', 'Đường dẫn thumbnail', 'trim|required|xss_clean');
                $this->form_validation->set_rules('slide_des', 'Mô tả slide', 'trim|required|xss_clean');
                $this->form_validation->set_rules('slide_order', 'Thứ tự slide', 'trim|required|numeric|greater_than[-1]|xss_clean');
                $this->form_validation->set_message('required','<span style="font-weight: bold">%s</span> không được để trống');
                $this->form_validation->set_message('greater_than','<span style="font-weight: bold">%s</span> phải lớn hơn 0');

                if($this->form_validation->run() == FALSE)
                {
                    $data['msg'] = '';
                }
                else
                {
                    $strUpdate = array(
                        'slide_link'        => $this->input->post('slide_link', TRUE),
                        'slide_image'       => $this->input->post('slide_image_edit', TRUE),
                        'slide_image_thumb' => $this->input->post('slide_image_thumb_edit', TRUE),
                        'slide_order'       => $this->input->post('slide_order', TRUE),
                        'slide_status'      => $this->input->post('slide_status', TRUE),
                        'slide_des'         => $this->input->post('slide_des', TRUE)
                    );

                    $check = $this->MSlide->edit($slide_id, $strUpdate);

                    if(!$check)
                    {
                        $data['msg'] = 'Lỗi cập nhật.';
                    }
                    else
                    {
                        //NEED CHECK//
                        if($this->input->post('slide_image_edit') != $data['slide']->slide_image):
                            if(is_file($data['slide']->slide_image)):
                                @unlink($data['slide']->slide_image);
                            endif;
                        endif;

                        if($this->input->post('slide_image_thumb_edit') != $data['slide']->slide_image_thumb):
                            if(is_file($data['slide']->slide_image_thumb)):
                                @unlink($data['slide']->slide_image_thumb);
                            endif;
                        endif;

                        $this->native_session->set_flashdata('msg','Cập nhật slide thành công.');

                        redirect(base_url()."backend/slide");

                        exit();
                    }
                }//End else
            }//End else

            $this->_data['content'] = $this->load->view("backend/slide/editslide", $data, TRUE);

            $this->load->view("backend/layout", $this->_data);
        }//End function editslide

        public function delslide()
        {
            if(!$this->uri->segment(4) || $this->uri->segment(4) == '')
            {
                show_404();

                exit();
            }

            $slide_id = $this->uri->segment(4);

            $check = $this->MSlide->delete($slide_id);

            if(!$check)
            {
                $this->native_session->set_flashdata('msg', 'Xoá không thành công');
            }
            else
            {
                $this->native_session->set_flashdata('msg', 'Xoá slide thành công');
            }

            redirect(base_url()."backend/slide");
        }//End function deleteslide

        public function uploadify()
        {
            $this->load->helper('uploadify');

    		$img_data = img_uploadify();

    		$size = getimagesize($img_data['full_path']);

            if($size[0] < 470 || $size[1] < 353)
            {
                @unlink($img_data['full_path']);
                echo 1;
            }
            else
            {
                $img_data = array(
                    'file_name' => $img_data['file_name'],
                    'width'     => $size[0],
                    'height'    => $size[1],
                    'fullpath'  => $img_data['full_path']
                );

                echo json_encode($img_data);
            }
        }//End function uploadify

        public function processimg()
        {
            /*Load jCrop helper*/
            $this->load->helper('jcrop');

            $thumb_image = jcrop();

            $img_parts = pathinfo($thumb_image);

            $img_data = array(
                'img_name'  => $img_parts['basename']
            );

            echo  json_encode($img_data);
        }//End processimg

        public function rejectimg()
        {
             if(is_file($this->input->post('filepath')))
                @unlink($this->input->post('filepath'));
        }//End rejectimg




	}

/* End of file Slide.php */
/* Location: ./controllers/backend/slide.php */